package com.example.demo;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;

public class SM4Util {
    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    private static final String ALGORITHM_NAME = "SM4";
    private static final int DEFAULT_KEY_SIZE = 128;
    private static final String CIPHER_ALGORITHM = "SM4/ECB/PKCS5Padding";

    // 生成随机密钥
    public static String generateKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM_NAME, "BC");
        keyGenerator.init(DEFAULT_KEY_SIZE);
        SecretKey secretKey = keyGenerator.generateKey();
        return Hex.toHexString(secretKey.getEncoded());
    }

    // 加密
    public static String encrypt(String data, String key) throws Exception {
        byte[] keyBytes = Hex.decode(key);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM_NAME);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] encrypted = cipher.doFinal(data.getBytes());
        return Hex.toHexString(encrypted);
    }

    // 解密
    public static String decrypt(String data, String key) throws Exception {
        byte[] keyBytes = Hex.decode(key);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM_NAME);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] decrypted = cipher.doFinal(Hex.decode(data));
        return new String(decrypted);
    }

    public static void main(String[] args) {
        try {
//            String key = generateKey();
            String key = "e5e6bba043d1db065c74d0947b7f82cc";
//            System.out.println("Generated Key: " + key);

//            String originalText = "{\"requestSource\":\"h5\",\"body\":{\"channelSource\":1,\"url\":\"https://m.517la.com/car/\"}}";
//            System.out.println("Original Text: " + originalText);
//
//            String encryptedText = encrypt(originalText, key);
//            System.out.println("Encrypted Text: " + encryptedText);

            String encryptedText = "b102c7b852c6229757ead3c208e3fdac317ad01811659484dd70a37dcd6668a1fde84e661e2f9486f82c8d6def45c5e54789b793933edf4e4d689c95a5496d6b8ab30e0de371071db863155e1132f08835b18d051cdc09937db91bf10d329e8ec717617035b895ee45a6386638ad3059b6c58ef8d09706f682a364735f0c39a660d7e17d3fa10589ca24f019f505c07eb560a8e5e9d7068ec14a405ecda5ec40621e6e6c87dffb3ab8fef5d354efb671a3cbbb3e71637025cb327ad61ab8fe0caefb58397c2fcf223d65d9b0758c13804838e6b4c7783dda52e9e34cdfd89bf9884ea4ff54d3274be08b85640e905b008f94e3e014ae5b5a36cf3aa7d0c6a6d2893b6173235c3ca8b89c13a912514a35";

            String decryptedText = decrypt(encryptedText, key);
            System.out.println("Decrypted Text: " + decryptedText);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
